<?php


namespace app\api\library\promotion\getPromotionUserQrCode;


use think\facade\Db;

class V
{
    public static function run($param)
    {
        $userId    = _checkEmptyParam($param[ 'userId' ], '用户不能为空');
        $promotion = Db::name('mall_promotion')->where('user_id', $userId)->find();
        if (empty($promotion[ 'qr_code' ] ?? '')) {
            $code = new \dh2y\qrcode\QRcode();
            $url  = 'userUpload/qrCode/' . md5($userId) . '.png';
            $code->png('https://tg.aipintuangou.com/h5/#/pages/index/index?promotionId=' . $userId, './' . $url);
            $realUrl = 'https://tg.aipintuangou.com/' . $url;
            if (false === Db::name('mall_promotion')->where('user_id', $userId)->update(['qr_code' => $realUrl])) {
                _outPut(204, [], '生成二维码失败');
            }
        } else {
            $realUrl = $promotion[ 'qr_code' ];
        }
        
        _outPut(200, $realUrl);
    }
}